#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int n = nums.size(), num = 0, left = -1, right = n - 1;
        int res = INT_MAX;
        int k = x;
        while (k > 0 && left + 1 < n) k -= nums[++left];
        if (k == 0)
            res = left + 1;
        for (; left >= 0; left--)
        {
            k += nums[left];
            while (k > 0 && right >= left) k -= nums[right--];
            if (k == 0)
                res = min(res, left + n - 1 - right);
        }
        return res == INT_MAX ? -1 : res;
    }
};